// package_shop/pages/order-list/order-list.js
import Api from '../../../utils/http/api'
import common_utils from "../../../utils/common_utils";
import time_utils from "../../../utils/time_utils";


const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderStatusList: [{//订单状态列表
        id: '0',
        title: '全部'
      },
      {
        id: '1',
        title: '待付款'
      },
      {
        id: '2',
        title: '已支付'
      },
      {
        id: '3',
        title: '待发货'
      },
      {
        id: '4',
        title: '待收货'
      },
      {
        id: '5',
        title: '已完成'
      },
      {
        id: '6',
        title: '退单'
      },
      {
        id: '7',
        title: '已退单'
      }

    ],
    orderList: [],//订单列表
    pageNum: 1,
    pageSize: 10,
    hasLoadMore: false,
    status: 0,//订单状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      status: options.status
    })
    this.getOrders()

  },
  //加载更多
  fecthMore() {
    if (this.data.hasLoadMore) {
      this.getOrders()
    }
  },
  //去详情页面
   onDetail(e) {
    let obj = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/package_shop/pages/order-detail/order-detail?id=' + obj.id,
    })
  },
  //点击tab
  onTabClick(e) {
    let status = e.detail.activeKey
    this.setData({
      status,
      hasLoadMore: false,
      orderList: [],
      pageNum: 1,
    })

    this.getOrders()
  },
  //获取订单列表数据
  getOrders() {
    this.setData({
      hasLoadMore: false
    })
    let params = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      status: this.data.status
    }
    Api.getOrders(params).then(res => {
      let list = res.data
      list.forEach((item) => {
        if (item.productPicture) {
          item.picture = common_utils.getApiImg(item.productPicture)
        }
      })
      this.setData({
        orderList: this.data.orderList.concat(list),
        pageNum: list.length >= this.data.pageSize ? this.data.pageNum + 1 : this.data.pageNum,
        hasLoadMore: list.length >= this.data.pageSize
      })




    }).catch(err => console.log(err))
  },

})
